package com.cy.store.service.impl;

import com.cy.store.service.ex.AccessDeniedException;
import com.cy.store.service.ex.AddressNotFoundException;
import com.cy.store.entity.Address;
import com.cy.store.mapper.AddressMapper;
import com.cy.store.service.IAddressService;
import com.cy.store.service.IDistrictService;
import com.cy.store.service.ex.AddressCountLimitException;
import com.cy.store.service.ex.DeleteException;
import com.cy.store.service.ex.InsertException;
import com.cy.store.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AddressServiceImpl implements IAddressService {
    @Value("${user.address.max-count}")
    private Integer maxCount;

    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private IDistrictService districtService;
    @Override
    public void addAddress(Integer uid, String username, Address address) {
        Integer count = addressMapper.countByUid(uid);
        if (count>=maxCount){
            throw new AddressCountLimitException("收货地址已达上限");
        }
        Integer fault = count== 0 ? 1 : 0 ;
        //补全信息
        address.setProvinceName(districtService.findByCode(address.getProvinceCode()));
        address.setCityName(districtService.findByCode(address.getCityCode()));
        address.setAreaName(districtService.findByCode(address.getAreaCode()));
        address.setIsDefault(fault);
        address.setUid(uid);
        address.setCreatedUser(username);
        address.setCreatedTime(new Date());
        address.setModifiedUser(username);
        address.setModifiedTime(new Date());
        //插入数据
        Integer rows = addressMapper.insert(address);
        if (rows!=1){
            throw new InsertException("修改收货地址时产生未知错误");
        }
    }

    @Override
    public List<Address> findByUid(Integer uid) {
        List<Address> addresses = addressMapper.findByUid(uid);
        for (Address address : addresses) {
            address.setCityCode(null);
            address.setAreaCode(null);
            address.setProvinceCode(null);
            address.setModifiedUser(null);
            address.setModifiedTime(null);
            address.setCreatedUser(null);
            address.setCreatedTime(null);
            //address.setUid(null);
            //address.setAid(null);
        }
        return addresses;
    }

    @Override
    public void changeDefault(Integer uid, Integer aid, String username) {
        Address result = addressMapper.findByAid(aid);
        if (result==null){
            throw new AddressNotFoundException("地址信息不存在");
        }
        if (!result.getAid().equals(aid)){
            throw new AccessDeniedException("非法访问");
        }
        Integer rows = addressMapper.updateIsDefaultByUid(uid);
        if (rows < 1 ){
            throw new UpdateException("更新数据产生未知异常");
        }
        Integer row = addressMapper.updateIsDefaultByAid(aid,username,new Date());
        if (row!=1){
            throw new UpdateException("更新数据产生未知异常");
        }
    }

    @Override
    public void deleteAddress(Integer uid, Integer aid, String username) {
        Address result = addressMapper.findByAid(aid);
        if (result == null){
            throw new AddressNotFoundException("地址信息不存在");
        }
        if (!result.getUid().equals(uid)){
            throw new AccessDeniedException("非法访问");
        }
        Integer rows = addressMapper.deleteByAid(aid);
        if (rows!=1){
            throw new DeleteException("删除时产生未知异常");
        }
        if (result.getIsDefault()==1){
            Address address = addressMapper.findByUidOrderByTime(uid);
            rows = addressMapper.updateIsDefaultByAid(address.getAid(), username, new Date());
            if (rows!=1){
                throw new UpdateException("修改默认收货地址时产生异常");
            }
        }
    }

    @Override
    public Address getByAid(Integer aid, Integer uid) {
        // 根据收货地址数据id，查询收货地址详情
        Address address = addressMapper.findByAid(aid);

        if (address == null) {
            throw new AddressNotFoundException("尝试访问的收货地址数据不存在");
        }
        if (!address.getUid().equals(uid)) {
            throw new AccessDeniedException("非法访问");
        }
        address.setProvinceCode(null);
        address.setCityCode(null);
        address.setAreaCode(null);
        address.setCreatedUser(null);
        address.setCreatedTime(null);
        address.setModifiedUser(null);
        address.setModifiedTime(null);
        return address;
    }
}
